//
//  SegView.m
//  ProjectsOne
//
//  Created by iOS开发 on 2018/7/9.
//  Copyright © 2018年 iOS开发. All rights reserved.
//

#import "SegView.h"

@implementation SegCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _label = [[UILabel alloc] init];
        _label.font = [UIFont systemFontOfSize:17];
        _label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_label];
        [_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_equalTo(self);
        }];
    }
    return self;
}

- (void)selectCell:(BOOL)isSelect title:(NSString *)str {
    _label.text = str;
    if (isSelect) {
        _label.textColor = [UIColor colorWithHexString:@"#ff0000"];
    } else {
        _label.textColor = [UIColor colorWithHexString:@"#999999"];
    }
}

@end


@interface SegView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collection;
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) NSIndexPath *sourceIndexPath;
@property (nonatomic, strong) UIView *scrollLine;
@end

@implementation SegView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (UICollectionView *)collection {
    if (!_collection) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(WIDTH / 2, 41);
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        
        _collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 41) collectionViewLayout:flowLayout];
        _collection.backgroundColor = [UIColor whiteColor];
        _collection.delegate = self;
        _collection.dataSource = self;
        _collection.showsVerticalScrollIndicator = NO;
        _collection.showsHorizontalScrollIndicator = NO;
        [self addSubview:_collection];
        
        [_collection registerClass:[SegCell class] forCellWithReuseIdentifier:@"segCell"];
    }
    return _collection;
}

- (instancetype)initWithFrame:(CGRect)frame Title:(NSArray *)titleArr {
    self = [super initWithFrame:frame];
    if (self) {
        self.titleArr = [NSArray arrayWithArray:titleArr];
        _sourceIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self createView];
    }
    return self;
}

- (void)createView {
    [self.collection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(self);
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor colorWithHexString:@"c5c5c5"];
    [self addSubview:line];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(.5f);
        make.left.bottom.right.mas_equalTo(self);
    }];
    
    UIView *middleLine = [[UIView alloc] init];
    middleLine.backgroundColor = [UIColor colorWithHexString:@"c5c5c5"];
    [self addSubview:middleLine];
    
    [middleLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self);
        make.width.mas_equalTo(.5f);
        make.height.mas_equalTo(25);
    }];
    
    _scrollLine = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width / 4, self.frame.size.height - 1, 80, 2)];
    _scrollLine.backgroundColor = [UIColor colorWithHexString:@"#db3100"];
    [self addSubview:_scrollLine];
    if (!_titleArr.count) {
        _collection.hidden = YES;
    }
    [_scrollLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(2);
        make.centerY.mas_equalTo(self.mas_bottom);
        make.centerX.mas_equalTo(-WIDTH / 4);
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.titleArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SegCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"segCell" forIndexPath:indexPath];
        
    if (indexPath == _sourceIndexPath) {
        [cell selectCell:YES title:_titleArr[indexPath.row]];
    } else {
        [cell selectCell:NO title:_titleArr[indexPath.row]];
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(WIDTH / 2, 41);
}

// 滑动联动
- (void)segAction:(NSInteger)idx {
    [self collectionView:_collection didSelectItemAtIndexPath:[NSIndexPath indexPathForItem:idx inSection:0]];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    // 改变按钮状态
    [self changeButtonStatus:collectionView indexPath:indexPath];
    // 改变横线
    [self scrollAnimation:collectionView indexPath:indexPath];
    
    if ([self.delegate respondsToSelector:@selector(clickSegIndex:)]) {
        [self.delegate clickSegIndex:indexPath.row];
    }
    
}
// 改变按钮状态
- (void)changeButtonStatus:(UICollectionView * _Nonnull)collectionView indexPath:(NSIndexPath * _Nonnull)indexPath {
    
    SegCell *cell = (SegCell *)[collectionView cellForItemAtIndexPath:_sourceIndexPath];
    [cell selectCell:NO title:_titleArr[_sourceIndexPath.row]];
    _sourceIndexPath = indexPath;
    SegCell *selectCell = (SegCell *)[collectionView cellForItemAtIndexPath:_sourceIndexPath];
    [selectCell selectCell:YES title:_titleArr[indexPath.row]];
}
// 改变横线
- (void)scrollAnimation:(UICollectionView * _Nonnull)collectionView indexPath:(NSIndexPath * _Nonnull)indexPath {
    
    SegCell *cell = (SegCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (!cell) {
        return;
    }
    [self.scrollLine mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(2);
        make.centerY.mas_equalTo(self.mas_bottom);
        make.centerX.mas_equalTo(cell);
    }];
    [UIView animateWithDuration:.2f animations:^{
        [self layoutIfNeeded];
    }];
}
@end
